package com.sky.controller.admin;

import com.alibaba.fastjson.JSON;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 功能：菜品
 * 作者：yedou
 * 日期：2025/3/3 17:00
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Resource
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("DishController.save.DishDTO:{}", JSON.toJSON(dishDTO));
            }
            dishService.saveWithFlavor(dishDTO);

            // 清理缓存数据
            String key = "dish_" + dishDTO.getCategoryId();
            cleanCache(key);

            return Result.success();
        }catch (Exception e){
            log.error("DishController.save:{}", e.getMessage());
            return Result.error("新增菜品失败");
        }

    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("DishController.page.dishPageQueryDTO:{}", JSON.toJSON(dishPageQueryDTO));
            }

            return Result.success(dishService.page(dishPageQueryDTO));
        }catch (Exception e){
            log.error("DishController.page:{}", e.getMessage());
            return Result.error("菜品分页查询失败");
        }
    }


    @ApiOperation("菜品批量删除")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long> ids){
        try {
            if(log.isInfoEnabled()){
                log.info("DishController.delete.ids:{}", ids);
            }
            dishService.deleteBatch(ids);
            // 清理缓存数据
            cleanCache("dish_*");

            return Result.success();
        }catch (Exception e){
            log.error("DishController.delete:{}", e.getMessage());
            return Result.error("菜品批量删除失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品和关联的口味数据")
    public Result<DishVO> getById(@PathVariable Long id){
        try {
            if(log.isInfoEnabled()){
                log.info("DishController.getById.id:{}", id);
            }

            return Result.success(dishService.getByIdWithFlavor(id));
        }catch (Exception e){
            log.error("DishController.getById:{}", e.getMessage());
            return Result.error("根据id查询菜品和关联的口味数据失败");
        }
    }

    @ApiOperation("修改菜品")
    @PutMapping()
    public Result update(@RequestBody DishDTO dishDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("DishController.update.dishDTO:{}", JSON.toJSON(dishDTO));
            }
            dishService.updateWithFlavor(dishDTO);
            // 清理缓存数据
            cleanCache("dish_*");
            return Result.success();
        }catch (Exception e){
            log.error("DishController.update:{}", e.getMessage());
            return Result.error("修改菜品失败");
        }
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> dishList = dishService.list(categoryId);
        return Result.success(dishList);
    }
}